//
//  YSGHelpCenterCategory.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/11/19.
//  Copyright © 2020 QDSG. All rights reserved.
//

import HandyJSON

struct YSGHelpCenterCategory: HandyJSON {
    var code: String?
    var name: String?
    var url: String?
}

struct YSGHelpCenterCategoryViewModel {
    let models: [YSGHelpCenterCategory]
    
    var numberOfRow: Int { models.count }
    
    func viewModel(at indexPath: IndexPath) -> YSGHelpCenterCategoryItemViewModel {
        return YSGHelpCenterCategoryItemViewModel(model: models[indexPath.item])
    }
    
    func flowLayout() -> UICollectionViewFlowLayout {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = itemSize()
        flowLayout.minimumLineSpacing = 36
        flowLayout.minimumInteritemSpacing = 18
        return flowLayout
    }
    
    func itemSize() -> CGSize {
        let itemW = (screenW - 18 * 4 - 16 * 2) / 3
        let itemH: CGFloat = 62
        return CGSize(width: itemW, height: itemH)
    }
    
    func cellHeight() -> CGFloat {
        let row = (numberOfRow - 1) / 3 + 1
        let collectionViewH = (itemSize().height + 36) * CGFloat(row) + 36 * 2
        let topHeaderH: CGFloat = 44
        return topHeaderH + collectionViewH
    }
    
    /// 获取帮助中心问题分类
    static func getHelpCenterCategory(finished: @escaping ([YSGHelpCenterCategory], String?, HttpCode) -> Void) {
        HttpTool.shared.request(target: ApiManager.helpCenterCategory, success: { (json, code) in
            YSGLog(json)
            if let models = JSONDeserializer<YSGHelpCenterCategory>.deserializeModelArrayFrom(json: json["data"].description) as? [YSGHelpCenterCategory] {
                finished(models, json["message"].description, code)
            } else {
                finished([], "数据解析错误，请重试", .failure)
            }
        }) { error in
            finished([], error, .failure)
        }
    }
    
    /// 提交意见反馈
    static func submitFeedback(feedback: String, finished: @escaping (String?, HttpCode) -> Void) {
        let params = ["feedback": feedback]
        HttpTool.shared.request(target: ApiManager.submitFeedback(paras: params), success: { (json, code) in
            finished(json["message"].description, code)
        }) { error in
            finished(error, .failure)
        }
    }
}

struct YSGHelpCenterCategoryItemViewModel {
    let model: YSGHelpCenterCategory
}
